/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the
 * confidential and proprietary information of Groupware.cc ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Groupware.cc.
 */
package com.rome.syncml.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.rome.syncml.domain.User;
import com.rome.syncml.exception.AuthenticationException;
import com.rome.syncml.service.UserService;

/**
 * 类LoginController.java的实现描述：TODO 类实现描述 
 * @author afei 2011-11-26 下午07:15:00
 */
@Controller
public class LoginController {

    private Logger log = Logger.getLogger(this.getClass());
    @Resource
    private UserService userService;
    @RequestMapping(value="/login")
    public String login(){
        return "login";
    }
    @RequestMapping(value="/loginto",method=RequestMethod.POST)
    public String loginto(HttpServletRequest request,@RequestParam(value="username",required=false)String userName,
                          @RequestParam(value="password",required=false)String password){
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            request.setAttribute("flg", 1);
            return "login";
        }
        User user = null;
        try {
            user = userService.authenticate(userName, password);
        } catch (AuthenticationException e) {
            log.error("Authenticatio error",e);
        }
        if(user==null){
            request.setAttribute("flg", 2);
            return "login";
        }else{
            request.getSession().setAttribute("user", user);
            return "redirect:/home";
        }
        
        
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "login";
    }
}
